package com.hafu.view.GYS;
import com.hafu.util.StringUtil;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.*;
import javax.swing.GroupLayout;
import javax.swing.table.*;
public class ManageGYSFrm extends JFrame{
    private GysInfoService gysInfoService = new GysInfoService();
    public ManageGYSFrm() {
        initComponents();
        fillTabel();
    }


    private void fillTabel() {
        ArrayList<GysInfo> gysInfoList = gysInfoService.getGysInfoByName(new GysInfo());
        DefaultTableModel model = (DefaultTableModel) table1.getModel();
        model.setRowCount(0);
        for (GysInfo gysInfo : gysInfoList) {
            Vector<Object> rowData = new Vector<>();
            rowData.add(gysInfo.getId());
            rowData.add(gysInfo.getName());
            rowData.add(gysInfo.getTel());
            model.addRow(rowData);
        }
    }
    private void button1(ActionEvent e){
        String name = textField1.getText();
        GysInfo gysInfo = new GysInfo();
        gysInfo.setName(name);
        ArrayList<GysInfo> gysInfoList = gysInfoService.getGysInfoByName(gysInfo);
        DefaultTableModel model =(DefaultTableModel) table1.getModel();
        model.setRowCount(0);
        if (gysInfoList.size()>0){
            for (GysInfo gysInfo1 : gysInfoList){
                Vector<Object> rowData = new Vector<>();
                rowData.add(gysInfo1.getId());
                rowData.add(gysInfo1.getName());
                rowData.add(gysInfo1.getTel());
                model.addRow(rowData);
            }
        } else{
            JOptionPane.showMessageDialog(null,"没有查询到供应商信息");
        }
    }
    private void table1MouseClicked(MouseEvent e){
        int row = table1.getSelectedRow();
        DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
        int chooseId = (int) dtm.getValueAt(row, 0);
        String name = (String) dtm.getValueAt(row, 1);
        String tel = (String) dtm.getValueAt(row, 2);
        textField2.setText(name);
        textField3.setText(tel);
    }
    private void button2(ActionEvent e){
        String name = textField2.getText();
        if (StringUtil.isEmpty(name)){
            JOptionPane.showMessageDialog(null,"供应商名称不能为空");
            return;
        }
        String tel = textField3.getText();
        if (StringUtil.isEmpty(tel)){
            JOptionPane.showMessageDialog(null,"供应商电话不能为空");
            return;
        }

        int row = table1.getSelectedRow();
        DefaultTableModel dtm =(DefaultTableModel) table1.getModel();
        int chooseId = (int) dtm.getValueAt(row, 0);
        GysInfo gysInfo = new GysInfo(chooseId, name, tel);
        Boolean aBoolean = gysInfoService.updateGysInfo(gysInfo);
        if(aBoolean){
            JOptionPane.showMessageDialog(null,"修改成功");
            this.dispose();
        }else {
            JOptionPane.showMessageDialog(null,"修改失败");
        }
    }
    private void button3(ActionEvent e) {
        int row =table1.getSelectedRow();
        DefaultTableModel dtm =(DefaultTableModel) table1.getModel();
        int chooseId=(int) dtm.getValueAt(row,0);
        Boolean isDeleteGysInfo = gysInfoService.deleteGysInfo(chooseId);
        if (isDeleteGysInfo){
            JOptionPane.showMessageDialog(null,"删除成功");
            fillTabel();
        }else {
            JOptionPane.showMessageDialog(null,"删除失败");
        }
    }


    private void initComponents(){
        label1 =new JLabel();
        textField1 = new JTextField();
        button1 = new JButton();
        scrollPane1=new JScrollPane();
        table1 = new JTable();
        label2 =new JLabel();
        textField2 = new JTextField();
        label3 = new JLabel();
        textField3 =new JTextField();
        button2 =new JButton();
        button3 =new JButton();
        setTitle("\u7ba1\u7406\u4f9b\u5e94\u5546");
        Container contentPane=getContentPane();
        label1.setText("\u4f9b\u5e94\u5546\u540d\uu5b57");
        button1.setText("\u67e5\u8be2");
        button1.addActionListener(e->button1(e));
        {
            table1.setModel(new DefaultTableModel(
                    new Object[][]{
                            {null, null, null},
                    },
                    new String[]{
                            "\u7f16\u53f7", "\u540d\u5b57", "\u7535\u8bdd"
                    }

            ));
            table1.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    table1MouseClicked(e);
                }
            });
            scrollPane1.setViewportView(table1);
        }
        label2.setText("\u4f9b\u5e94\u5546\u540d\u79f0");
        label3.setText("\u4f9b\u5e94\u5546\u7535\u8bdd");
        button2.setText("\u4fee\u6539");
        button2.addActionListener(e->button2(e));
        button3.setText("\u5220\u9664");
        button3.addActionListener(e->button3(e));
        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
                contentPaneLayout.createParallelGroup()
                        .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                                .addContainerGap(106, Short.MAX_VALUE)
                                .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 556, GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(201,Short.MAX_VALUE))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addGap(41,41,41)
                                .addGroup(contentPaneLayout.createParallelGroup()
                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                .addComponent(label1, GroupLayout.PREFERRED_SIZE, 91, GroupLayout.PREFERRED_SIZE)
                                                .addGap(41,41,41)
                                                .addComponent(textField1, GroupLayout.PREFERRED_SIZE, 162, GroupLayout.PREFERRED_SIZE)
                                                .addGap(185,185,185)
                                                .addComponent(button1))
                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                .addGroup(contentPaneLayout.createParallelGroup()
                                                        .addComponent(label3)
                                                        .addComponent(label2,GroupLayout.PREFERRED_SIZE,109, GroupLayout.PREFERRED_SIZE)
                                                        .addGap(35,35,35)
                                                        .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                                                .addComponent(textField2, GroupLayout.DEFAULT_SIZE,146,Short.MAX_VALUE)
                                                                .addComponent(textField3, GroupLayout.DEFAULT_SIZE,146,Short.MAX_VALUE)
                                                                .addGap(156,156,156)
                                                                .addGroup(contentPaneLayout.createParallelGroup()
                                                                        .addComponent(button3)
                                                                        .addComponent(button2))))
                                                .addContainerGap(265,Short.MAX_VALUE))
                                )));

        contentPaneLayout.setVerticalGroup(
                contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addGap(22,22,22)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(button1)
                                        .addComponent(label1)
                                        .addComponent(textField1,GroupLayout.PREFERRED_SIZE,GroupLayout.DEFAULT_SIZE,
                                                GroupLayout.PREFERRED_SIZE))
                                .addGap(18,18,18)
                                .addComponent(scrollPane1,GroupLayout.PREFERRED_SIZE,226,GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED,85,Short.MAX_VALUE)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(button2)
                                        .addComponent(label2)
                                        .addComponent(textField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,GroupLayout.PREFERRED_SIZE))
                                .addGap(33,33,33)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(label3)
                                        .addComponent(button3)
                                        .addComponent(textField3,GroupLayout.PREFERRED_SIZE,GroupLayout.DEFAULT_SIZE,GroupLayout.PREFERRED_SIZE))
                                .addGap(62,62,62))
        );
        pack();
        setLocationRelativeTo(getOwner());
    }


    private JLabel label1;
    private JTextField textField1;
    private JButton button1;
    private JScrollPane scrollPane1;
    private JTable table1;
    private JLabel label2;
    private JTextField textField2;
    private JLabel label3;
    private JTextField textField3;
    private JButton button2;
    private JButton button3;

}